<?php
/**
 * @title 5524. 经营摩天轮的最大利润 - 迭代优化
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }


class Solution {

    /**
     * @param Integer[] $customers
     * @param Integer $boardingCost
     * @param Integer $runningCost
     * @return Integer
     */
    function minOperationsMaxProfit($customers, $boardingCost, $runningCost) {
        /**
         * @since 2020-09-27 最大利润, 次数
         */
        $maxProfit = 0;
        $times = -1;

        /**
         * @since 2020-09-27 累计利润
         */
        $sumProfit = 0;
        $sumTimes = 0;

        /**
         * @since 2020-09-27 4个人利润
         */
        $profit4 = $boardingCost*4-$runningCost;
        if($profit4 <= 0){
            return -1;
        } else {}

        /**
         * @since 2020-09-27 4人, 3人, 2人, 1人, 0人利润
         */
        $profits = [
            4 => $boardingCost*4-$runningCost,
            3 => $boardingCost*3-$runningCost,
            2 => $boardingCost*2-$runningCost,
            1 => $boardingCost-$runningCost,
            0 => 0-$runningCost,
        ];
        // return $profits;

        /**
         * @since 2020-09-27 上批留下等外客人
         */
        $lastWaitCustomers = 0;

        /**
         * @since 2020-09-27 依次遍历
         */
        foreach ($customers as $num){
            $num += $lastWaitCustomers;
            if($num >= 4){
                /**
                 * @since 2020-09-27 最多4人
                 */
                $lastWaitCustomers = $num - 4;

                /**
                 * @since 2020-09-27 累计利润
                 */
                $sumTimes++;
                $sumProfit += $profits[4];

                /**
                 * @since 2020-09-27 更新最大利润, 开头已判断是否为正
                 */
                $maxProfit = $sumProfit;
                $times = $sumTimes;
            } else {
                /**
                 * @since 2020-09-27 一次乘坐完
                 */
                $lastWaitCustomers = 0;

                /**
                 * @since 2020-09-27 累计利润
                 */
                $sumTimes++;
                $sumProfit += $profits[$num];

                /**
                 * @since 2020-09-27 是否最大利润
                 */
                if($sumProfit > $maxProfit){
                    $maxProfit = $sumProfit;
                    $times = $sumTimes;
                } else {}
            }
        }
        // return $lastWaitCustomers;

        /**
         * @since 2020-09-27 收官, 最后一批客人
         */
        if($lastWaitCustomers > 4){
            $n = $lastWaitCustomers >> 2;
            $lastWaitCustomers = $lastWaitCustomers % 4;

            /**
             * @since 2020-09-27 累计利润
             */
            $sumProfit += $n*$profits[4];
            $sumTimes += $n;

            /**
             * @since 2020-09-27 是否最大利润
             */
            if($sumProfit > $maxProfit){
                $maxProfit = $sumProfit;
                $times = $sumTimes;
            } else {}
        }

        /**
         * @since 2020-09-27 最后一次不足4人
         */
        if($lastWaitCustomers > 0){
            $sumProfit += $profits[$lastWaitCustomers];
            $sumTimes++;

            /**
             * @since 2020-09-27 是否最大利润
             */
            if($sumProfit > $maxProfit){
                $maxProfit = $sumProfit;
                $times = $sumTimes;
            } else {}
        } else {}

        /**
         * @return
         */
        return $times;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202009/2020.09.27_5.php
 */
$datas = [
    // [
    //     [8,3], 5, 6
    // ],
    // [
    //     [10,9,6], 6, 4,
    // ],
    [
        [19,42,25,18,34,24,22,11,38,33,50,33,50,26,10,4,46,5,3,50,38,20,13,1,28,6,37,11,1,9,39,13,4,14,3,39,39,0,16,11,12,16,2,28,46,33,49,43,9,23,43,40],
        35,
        96,
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->minOperationsMaxProfit(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
